/*本文件用来存放棋盘操作相关函数
 * 初始化棋盘
 * 揭晓敌方棋盘布局
 */

/*初始化棋盘*/
function boardInit(){
	/*初始化我方棋盘*/
	var myBoard = mui('#myBoard')[0];
  	var boardStr = '<table>';
  	for (var i = 0; i < 10; i++) {
  		boardStr += '<tr id="myRow'+i+'">';
  		for (var j = 0; j < 10; j++) {
  			var k = i*10+j;
  			boardStr += '<td><button id="myBtn'+k+'" class="boardButton" onclick="boardBtnClick(this)"></button></td>';
  		}
  		if (i==0) {
			boardStr += '<th rowspan="2"><button type="button" id="upBtn" class="funButton" onclick="funBtnClick(this)">Up</button></th>';
  		}
  		if (i==2) {
			boardStr += '<th rowspan="2"><button type="button" id="leftBtn" class="funButton" onclick="funBtnClick(this)">Left</button></th>';
  		}
  		if (i==4) {
			boardStr += '<th rowspan="2"><button type="button" id="rightBtn" class="funButton" onclick="funBtnClick(this)">Right</button></th>';
  		}
  		if (i==6) {
			boardStr += '<th rowspan="2"><button type="button" id="downBtn" class="funButton" onclick="funBtnClick(this)">Down</button></th>';
  		}
  		if (i==8) {
			boardStr += '<th rowspan="2"><button type="button" id="delBtn" class="funButton" onclick="funBtnClick(this)">Del</button></th>';
  		}
  		boardStr += '</tr>';
  	}
  	boardStr += '</table>';
  	myBoard.innerHTML = boardStr;
  	
  	/*初始化敌方棋盘*/
  	var enemyBoard = mui('#enemyBoard')[0];
  	var boardStr = '<table>';
  	for (var i = 0; i < 10; i++) {
  		boardStr += '<tr id="enemyRow'+i+'">';
  		for (var j = 0; j < 10; j++) {
  			var k = i*10+j;
  			boardStr += '<td><button id="enemyBtn'+k+'" class="boardButton" onclick="boardBtnClick(this)"></button></td>';
  		}
  		if (i==0) {
			boardStr += '<th rowspan="2"><button type="button" id="readyBtn" class="funButton" onclick="readyBtnClick(this)">Ready</button></th>';
		}
  		if (i==2) {
			boardStr += '<th rowspan="2"><button type="button" id="shootBtn" class="funButton" onclick="shootBtnClick(this)">Shoot</button></th>';
		}
  		if (i==4) {
			boardStr += '<th rowspan="2"><button type="button" id="markBtn" class="funButton" onclick="markBtnClick(this)">Mark</button></th>';
		}
  		if (i==6) {
			boardStr += '<th rowspan="2"><button type="button" id="replayBtn" class="funButton" onclick="replayBtnClick(this)">Replay</button></th>';
		}
  		if (i==8) {
			boardStr += '<th rowspan="2"><button type="button" id="helpBtn" class="funButton" onclick="helpBtnClick(this)">Help</button></th>';
		}
  		boardStr += '</tr>';
  	}
  	boardStr += '</table>';
  	enemyBoard.innerHTML = boardStr;
}

/*揭晓对方布局*/
function enemyBoardUncover(){
	var enemyBtn;
	mui('#shootBtn')[0].disabled = 'disabled';
	mui('#markBtn')[0].disabled = 'disabled';
	//揭露敌方机头位置
	for (var i = 0; i < enemyFlightCount; i++){
		var x = enemyFlight[i][0];
		var y = enemyFlight[i][1];
		var index = y*10+x;
		enemyBtn = mui('#enemyBtn'+index)[0];
		enemyBtn.style.backgroundColor = 'red';
		shootMap[x][y] = true;
	}
	enemyBtnUncover();
}

/*揭晓按钮*/
function enemyBtnUncover(){
	if (count==100) return;
	var x = count%10;
	var y = parseInt(count/10);
	enemyBtn = mui('#enemyBtn'+count)[0];
	if (!shootMap[x][y]) enemyBtn.disabled = 'disabled';
	count++;
	if (!shootMap[x][y]) {
		if(enemyBoard[x][y]){
			enemyBtn.style.backgroundColor = 'yellow';
		}
		else{
			enemyBtn.style.backgroundColor = 'blue';
		}
	}
	setTimeout("enemyBtnUncover();",5);
}